package com.bw.aop;

import com.bw.domain.SysUser;
import com.bw.domain.Tlog;
import com.bw.service.SysUserService;
import com.bw.service.TlogService;
import com.bw.utils.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

//@Slf4j
//@Component
//@Aspect
public class MyAop {
    @Autowired
    TlogService tLogService;
    @Resource
    HttpServletRequest request;
    @Autowired
    SysUserService sysUserService;
    @Around("execution(* com.bw.controller.AuthController.login(..))")
    public Result around(ProceedingJoinPoint joinPoint) throws Throwable {
        String ip = request.getRemoteAddr();
        //时间
        Date requestime = new Date();
        //地址
        String url = request.getRequestURI();
        SysUser sysUser=null;
        Result result=null;
        String status="0";
        try {
            //用户名
            Object[] args = joinPoint.getArgs();
            sysUser= (SysUser) args[0];
            String username = sysUser.getUsername();
            //执行原方法
           result= (Result) joinPoint.proceed();
            if(result.getCode()==200){
                status="1";
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            Tlog tlog = new Tlog(null,sysUser.getUsername(),status,ip,url,requestime);
            tLogService.save(tlog);
            return  result;
        }


//        log.info("ip:{},username:{},url:{},status:{},requestTime:{},name:{}",ip,username,url,status,requestime,null);

    }
}
